package com.zh.rent.controller.backstage;

import com.github.pagehelper.PageInfo;
import com.zh.rent.pojo.Account;
import com.zh.rent.pojo.StandbyAccount;
import com.zh.rent.service.AccountService;
import com.zh.rent.service.StanAccountService;
import com.zh.rent.util.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/bacaccountStan")
@Api(tags="备用账号后台管理")
public class BacAccountStanControll {
    @Autowired
    StanAccountService  StanAccountServiceImpl;
    @Autowired
    AccountService AccountServiceImpl;

    @PostMapping("/bacStanAccaddselAll")
    @ResponseBody
    @ApiOperation(value = "备用账号显示")
    public JsonResult bacAccaddselAll(@RequestParam(value="pageSize",defaultValue="5")Integer pageSize,
                                      @RequestParam(value="pageNum",defaultValue="1")Integer pageNum,
                                      String name) {
        Map<String,Object> map = new HashMap<>();
        name = "%"+name+"%";
        map.put("name",name);
        PageInfo<StandbyAccount> mapPageInfo=null;
        mapPageInfo = StanAccountServiceImpl.selaccountAll(pageNum, pageSize, map);
        for (StandbyAccount standbyAccount : mapPageInfo.getList()) {
            if(standbyAccount.getStart()==0) standbyAccount.setStartText("否");
            if(standbyAccount.getStart()==1) standbyAccount.setStartText("是");
        }
        return JsonResult.OK(mapPageInfo);
    }


    @PostMapping("/bacStanAccaddAll")
    @ResponseBody
    @ApiOperation(value = "账号导入")
    public JsonResult bacStanAccaddAll(@RequestBody List<StandbyAccount> accounts) {
        List<StandbyAccount> accoun =new ArrayList<>();
        for (StandbyAccount account1 : accounts) {
            System.out.println("他妈爱妈妈："+account1.toString());
            StandbyAccount account = new StandbyAccount();
            account.setAccountName(account1.getAccountName());//账号名称
            account.setAccountpwd(account1.getAccountpwd());//账号密码
            account.setAccountType(account1.getAccountType());//账号类型
            account.setExpireDays(account1.getExpireDays());//天数
            account.setMailboxName(account1.getMailboxName());//邮箱名称
            account.setMailboxPwd(account1.getMailboxPwd());//邮箱密码
            account.setAuthorization(account1.getAuthorization());//邮箱授权码
            account.setHost(account1.getHost());//邮箱服务器地址
            account.setRemark(account1.getRemark());//备注
            accoun.add(account);
        }
        int insertaccounts = StanAccountServiceImpl.insertaccountsAll(accoun);
        if(insertaccounts==0){
            return JsonResult.ERROR("导入失败");
        }
        return JsonResult.OK("导入成功");
    }


    @PostMapping("/bacStanAccadd")
    @ResponseBody
    @ApiOperation(value = "单个备用账号添加")
    public JsonResult bacStanAccadd( String AccountName,
                                 String Accountpwd,
                                 String AccountType,
                                 Integer ExpireDays,
                                 String MailboxName,
                                 String MailboxPwd,
                                 String Authorization,
                                 String Host,
                                 String Remark) {
        StandbyAccount account=new StandbyAccount();
        account.setAccountName(AccountName);//账号名称
        account.setAccountpwd(Accountpwd);//账号密码
        account.setAccountType(AccountType);//账号类型
        account.setExpireDays(ExpireDays);//天数
        account.setMailboxName(MailboxName);//邮箱名称
        account.setMailboxPwd(MailboxPwd);//邮箱密码
        account.setAuthorization(Authorization);//邮箱授权码
        account.setHost(Host);//邮箱服务器地址
        account.setRemark(Remark);//备注
        //account.setStandbyid(null);//备用账号id
        int insertaccounts = StanAccountServiceImpl.insertaccounts(account);
        if(insertaccounts==0){
            return JsonResult.ERROR("导入失败");
        }
        return JsonResult.OK("导入成功");
    }

    @PostMapping("/bacAccaddupdate")
    @ResponseBody
    @ApiOperation(value = "账号修改")
    public JsonResult bacAccaddupdate(Integer Standbyid,
                                      String AccountName,
                                      String Accountpwd,
                                      String AccountType,
                                      Integer ExpireDays,
                                      String MailboxName,
                                      String MailboxPwd,
                                      String Authorization,
                                      String Host,
                                      String Remark,
                                      Integer Start) {
        StandbyAccount account = new StandbyAccount();
        account.setStandbyid(Standbyid);//账号id
        account.setAccountName(AccountName);//账号名称
        account.setAccountpwd(Accountpwd);//账号密码
        account.setAccountType(AccountType);//账号类型
        account.setExpireDays(ExpireDays);//天数
        account.setMailboxName(MailboxName);//邮箱名称
        account.setMailboxPwd(MailboxPwd);//邮箱密码
        account.setAuthorization(Authorization);//邮箱授权码
        account.setHost(Host);//邮箱服务器地址
        account.setRemark(Remark);//备注
        account.setStart(Start);//是否启用

        System.out.println("44444:"+account.toString());
        //修改
        int updateaccount = StanAccountServiceImpl.updateaccount(account);
        if(updateaccount==0){
            return JsonResult.ERROR("修改失败");
        }
        return JsonResult.OK("修改成功");

    }

    @PostMapping("/StanAccBacdeleteAll")
    @ResponseBody
    @ApiOperation(value = "账号表根据id批量删除")
    public JsonResult StanAccBacdeleteAll(String Standbyid) {
        try {
            String buy= Standbyid.substring(1,Standbyid.length()-1);
            List<Integer> list = new ArrayList<Integer>();
            String[] str = buy.split(",");
            for (int i = 0; i < str.length; i++) {
                int i1 = Integer.parseInt(str[i]);
                list.add(i1);
                List<Account> accounts = AccountServiceImpl.selStanAid(i1);
                for (Account account : accounts) {
                    Integer  a= null;
                    int i2 = AccountServiceImpl.updateStan(a, account.getAccountid());
                }
            }
            Integer i = StanAccountServiceImpl.delAccBccAll(list);
            return JsonResult.OK("删除成功");
        }catch (Exception e){
            return JsonResult.ERROR("删除失败");
        }
    }

    @PostMapping("/StanAccBacdelete")
    @ResponseBody
    @ApiOperation(value = "账号表根据id删除")
    public JsonResult StanAccBacdelete(Integer Standbyid) {
        try {
            Integer i = StanAccountServiceImpl.delAccBccid(Standbyid);
            List<Account> accounts = AccountServiceImpl.selStanAid(Standbyid);
            for (Account account : accounts) {
                Integer  a= null;
                int i2 = AccountServiceImpl.updateStan(a, account.getAccountid());
            }
            return JsonResult.OK("删除成功");
        }catch (Exception e){
            return JsonResult.ERROR("删除失败");
        }
    }
}
